﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Facebook;
using System.Drawing;

namespace lydoe.qua2010
{
    public partial class _default : System.Web.UI.Page
    {
        private string AppId = "316484738458897";//quà 20/10
        private string AppSecret = "0eb61b1597ea6a0d636bf4809e1bdf56";

        private string PageId = "332778643418527";//vlhanoi.com.vn
        //private string PageId = "447811898603845";//test app

        protected void Page_Load(object sender, EventArgs e)
        {
            string host = Request.ServerVariables["HTTP_HOST"];
            if (Request.QueryString["code"] != null)
            {
                var cl = new FacebookOAuthClient();
                cl.AppId = AppId;
                cl.AppSecret = AppSecret;
                cl.RedirectUri = new Uri("http://" + host + "/qua2010/default.aspx");

                var code = (IDictionary<string, object>)cl.ExchangeCodeForAccessToken(Request.QueryString["code"]);

                var fb = new Facebook.FacebookClient((string)code["access_token"]);

                var me = (IDictionary<string, object>)fb.Get("/me");
                //
                
                //
                //bool isGirl = (string)me["gender"] == "female";
                if (facebookInt.GetLikePage(fb, PageId))
                {
                    Response.Write("<script type=\"text/javascript\">alert(\"like rùi nhé!\");</script>");
                }
                else
                {
                    Response.Write("<script type=\"text/javascript\">alert(\"chưa thèm like ah??!\");</script>");
                }
                return;
                bool isGirl = true;
                if (isGirl)
                {
                    //Random rnd = new Random();
                    //int n = rnd.Next(1, 20);
                    //string sbg = "/images/" + n.ToString() + ".jpg";
                    string sbg = "/qua2010/images/1.jpg";//test
                    sbg = Server.MapPath(sbg);

                    TextDraw oText = new TextDraw();
                    oText.ix = 130;
                    oText.iy = 20;
                    oText.sText = (string) me["name"];
                    oText.sFont = "Arial";
                    oText.iSize = 12;
                    oText.bBold = true;
                    //oText.brushes = Brushes.Violet;

                    var path = ImageHandle.ImageOver("https://graph.facebook.com/" + (string)me["id"] + "/picture?type=large", (string)me["id"], sbg, Server.MapPath("/qua2010/out"), 15, 15, 100, 100, oText);

                    var ex = new Dictionary<string, object>();
                    var facebookMedia = new FacebookMediaObject
                    {
                        ContentType = "image/jpg",
                        FileName = path
                    };

                    //var tags = new[] 
                    //{ 
                    //    new { tag_uid = friendId_1, x = 20, y = 20 },
                    //    new { tag_uid = friendId_2, x = 40, y = 40 },
                    //    new { tag_uid = (string)me.id, x = 60, y = 60 }
                    //};

                    var bytes = File.ReadAllBytes(path);
                    facebookMedia.SetValue(bytes);
                    ex["source"] = facebookMedia;
                    //ex.tags = tags;
                    ex["message"] = "Các bạn nữ vào nhận quà 20/10 nào " + @"http://www.facebook.com/apps/application.php?id=" + AppId;
                    var result = fb.Post("me/photos", ex);
                    Response.Write("<script type=\"text/javascript\">alert(\"Thành công rùi đó! Xem trên tường bạn nhé!\");</script>");
                }
                else
                {
                    Response.Write("<script type=\"text/javascript\">alert(\"sorry ứng dụng này chỉ giành cho nữ!\");</script>");
                }


                //File.Delete(path);
                //var c = (JsonObject)fb.Get("me/posts");
                //var dn = (JsonObject)fb.Get("/me/friends?fields=id,name,birthday");
                //var listFriend = new List<User>();
                //var stb = new StringBuilder();
                //stb.AppendLine("Tổng số trong danh sách có " + ((JsonArray)dn["data"]).Count + "bạn!");
                //foreach (var friend in (JsonArray)dn["data"])
                //{
                //    // var user = new User();
                //    stb.AppendLine("ID: " + ((JsonObject)friend)["id"] + "\t Tên: " + ((JsonObject)friend)["name"] +
                //                   "\t Ngày sinh(nếu có): " + ((JsonObject)friend)["birthday"]);
                //    // listFriend.Add(user);
                //}
                //dynamic me = new ExpandoObject();
                //me.message = stb.ToString();
                //dynamic d = fb.Post("/me/feed", me);
                //File.WriteAllText("D:\\facebook.txt", c.ToString(), Encoding.UTF8);
            }
            else
            {
                string spath = @"https://graph.facebook.com/oauth/authorize?client_id=" + AppId +
                                "&redirect_uri=http://" + host +
                               @"/qua2010/default.aspx&scope=publish_stream";
                Response.Redirect(spath);

                //Response.Redirect(@"https://graph.facebook.com/oauth/authorize?client_id=" +
                //              "277864275663763" + "&redirect_uri=http://" + host +
                //              @"/default.aspx&scope=publish_stream,user_birthday,email");
            }
        }
    }
}
